<?php
/**
 * WMS连接客户端
 *
 * PHP version 5
 * 
 * @package    Project
 * @subpackage Ecga
 * @author     v.k <none@none.com> 
 * @copyright  2010 Xingchangxinda Inc.
 * @version    SVN: $Id$
 */
namespace Project\Apps\Ecga\Library;

use \Framework\System\Rest\RestClient;

/**
 * WMS客户端
 *
 * PHP version 5
 *
 * @package    Project
 * @subpackage Ecga
 * @author     v.k <none@none.com> 
 * @copyright  2010 Xingchangxinda Inc.x
 */
class WmsClient
{
    
    /**
     * WMS接口用户凭证
     */
    const WMS_AUTH_USER = '18900';
    const WMS_AUTH_PASSWORD = '18900test';
    
    /**
     * WMS接口类型
     */
    const WMS_SERVICE_STATISTICS = 'statistics';
    
    /**
     * WMS接口地址
     */
    static $wms_services = array(
        self::WMS_SERVICE_STATISTICS => 'http://wms.ec3s.com/index.php/api/statistics'
    );
    
    /**
     * WMS接口类型
     */
    protected $interfaceType = null;
    
    /**
     * 构造
     *
     */
    private function __construct()
    {
        //
    }
    
    /**
     * 发送接口请求
     *
     * @param string $interface 请求的接口名称
     * @param array  $params    接口所需参数
     * @param string $method    请求的方法(GET, PUT, ...)
     * 
     * @return object
     */
    public function request($interface, $params = array(), $method = 'GET')
    {
        $response = RestClient::call(
            $method, $url = sprintf('%s/%s', self::$wms_services[$this->interfaceType], $interface),
            $params, self::WMS_AUTH_USER, self::WMS_AUTH_PASSWORD
        )->getResponse();
                
        return json_decode($response, true);
    }
    
    /**
     * 取wms client instance
     * 
     * @param string $interfaceType 接口类型
     *
     * @return WmsClient
     */
    public static function getInstance($interfaceType = self::WMS_SERVICE_STATISTICS)
    {
        static $instance = null;
        
        if (null === $instance) {
            $instance = new self;
        }
        
        $instance->interfaceType = $interfaceType;
        
        return $instance;
    }
}